	 /*
      * Jquery—addShopping   0.1
      * Copyright (c) 2016  随风丶小柏     QQ：1101470551
      * Date: 2016-08-12
      * 使用Jquery—shopping实现一个简单的加入购物车效果
	 */
(function($){
	var defaults = {
		endElement:"",
		iconCSS:"",
		iconImg:"",
		endFunction:function(element){
			return false;
		}
	};
	
	$.extend($.fn,{
		shoping:function(options){
			var self=this,
				$options = $.extend(defaults,options);
				if($options.endElement=="" || $options.endElement==null) throw new Error("结束节点为必填字段");
			var $endElement = $($options.endElement);
			var S={
				init:function(){
					$(self).on('click',this.addShoping);
				},
				addShoping:function(e){
                   var add_number=$("#number1").val();
					if(add_number==undefined){
						add_number=$("#qty_item_1").val();
					}
					if(add_number==undefined){
						add_number=1;
					}
					var goods_sn=$("#goods_sn").val();
					if(goods_sn==undefined){
						goods_sn='';
					}
                    //加入购物车
					$.ajax({
						type: 'post',
						url:add_cart_url,
						dataType: 'json',
						data:{'goods_id':$(this).attr('data'),'number':add_number,'goods_sn':goods_sn},
						async:false,//
						cache:true,
						success: function(result){
							var msg=result.msg;
							if(result.ret_code==0){

                               if(msg.indexOf('登录')>-1){
								   modalBG = $('.reveal-modal-bg');
								   if(modalBG.length == 0) {
									   modalBG = $('<div class="reveal-modal-bg" />').insertAfter($("#gotop"));
								   }
								   $(".reveal-modal-bg").css("display",'block');
								   if($("#meidl").length>0){
									   $("#meidl").css("display",'block');
									   $("#meidl").css("visibility",'visible');
								   }

							   }else{
								   alert(result.msg);
							   }



							}else{


								e.stopPropagation();
								var $target=$(e.target),
									x = $target.offset().left + 30,
									y = $target.offset().top + 10,
									X = $endElement.offset().left,
									Y = $endElement.offset().top;
								if(!($(document).find("#cartIcon").length>0)){
									$('body').append(S.addIcon);
									var $obj=$('#cartIcon');
									if(!$obj.is(':animated')){
										$obj.css({'left': x,'top': y}).animate({'left': X,'top': Y+70},500,function() {
											$obj.stop(false, false).animate({'top': Y-20,'opacity':0},500,function(){
												$obj.fadeOut(300,function(){
													$obj.remove();
													$target.data('click',false);
													$options.endFunction($(this),result.data);
												});
											});
										});
									};
								}

								//新增增加购物车数量
								//end


							}

						},

					});
					//end
					//e.stopPropagation();
					//var $target=$(e.target),
					//    x = $target.offset().left + 30,
					//	y = $target.offset().top + 10,
					//	X = $endElement.offset().left,
					//	Y = $endElement.offset().top;
					//	if(!($(document).find("#cartIcon").length>0)){
					//		$('body').append(S.addIcon);
					//		var $obj=$('#cartIcon');
					//		if(!$obj.is(':animated')){
					//			$obj.css({'left': x,'top': y}).animate({'left': X,'top': Y+70},500,function() {
					//				$obj.stop(false, false).animate({'top': Y-20,'opacity':0},500,function(){
					//					$obj.fadeOut(300,function(){
					//						$obj.remove();
					//						$target.data('click',false);
					//						$options.endFunction($(this));
					//					});
					//				});
					//			});
					//		};
					//	}
				},
				addIcon:function(){
					if ($options.iconImg=="" || $options.iconImg==null) {
						throw new Error("样式图片必须填上");
					} 
					var icon = '<div id="cartIcon" style="width:32px;height:32px;padding:0px;overflow:hidden;position:absolute;z-index:890;'+$options.iconCSS+'"><img src="'+$options.iconImg+'" width="32" height="32" /></div>';
					return icon;
				}
			};
			S.init(); 
		}
	});	
})(jQuery)